//
//  NewspaperPicDesViewController.m
//  JianChaRiBao
//
//  Created by 常兴宇 on 13-10-30.
//  Copyright (c) 2013年 wzyk. All rights reserved.
//

#import "NewspaperPicDesViewController.h"
#import "NewspaperTitleCell.h"
#import "NewspaperDesCell.h"
#import "NewspaperSingleArticleViewController.h"
#import "AppDelegate.h"
@interface NewspaperPicDesViewController ()
{
    NSArray * mDataArray;
}
@end

@implementation NewspaperPicDesViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        shouldSwipeBack = YES;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    titleLabel.scrollType = ScrollSingle;
    titleLabel.font = [UIFont systemFontOfSize:21.0f];
    titleLabel.text = [NSString stringWithFormat:@"中国保险报%@",self.titleStr];
    [titleLabel startScroll];
    
    [self getData];
}
-(void)getData{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        id obj = [[DataRequest sharedRequest] getNewspaperPicDes:self.item_id pageNum:[self.pageNumber integerValue]];
        NSLog(@"obj:%@",obj);

        dispatch_async(dispatch_get_main_queue(), ^{
            if ([obj isKindOfClass:[NSDictionary class]] && [[obj objectForKey:@"result"] isKindOfClass:[NSArray class]]) {
                NSArray * array = [obj objectForKey:@"result"];
                if (array.count == 1 && [[[array objectAtIndex:0] objectForKey:@"status_code"] integerValue] == 0) {
                    ABPromptView * abp = [[ABPromptView alloc]initWithTitle:@"该版下不包含文章"];
                    [abp show];
                    [abp release];
                }else{
                    mDataArray = [[NSArray alloc]initWithArray:array];
                    [mTableView reloadData];
                }
                

            }
        });
    });
}


- (IBAction)backButtonClicked:(id)sender {
    NSLog(@"rootNavController>>>>%@",[UIApplication rootNavController]);
    [[UIApplication rootNavController] popViewControllerAnimated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return mDataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary * dict = [mDataArray objectAtIndex:indexPath.row];
    if ([[dict objectForKey:@"is_page"] boolValue]) {
        static NSString * cellID = @"cellID";
        NewspaperTitleCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"NewspaperTitleCell" owner:self options:0]lastObject];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.baseInfo = dict;
        [cell updateDisplay];
        return cell;
        
    }else{
        static NSString * cellID = @"cellID1";
        NewspaperDesCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"NewspaperDesCell" owner:self options:0]lastObject];
        }
        
        cell.baseInfo = dict;
        [cell updateDisplay];
        return cell;
    }
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary * dict = [mDataArray objectAtIndex:indexPath.row];
    if ([[dict objectForKey:@"is_page"] boolValue]) {
        return 40;
    }else{
        CGSize size = [[dict objectForKey:@"page_name"] sizeWithFont:[UIFont systemFontOfSize:17.0f] constrainedToSize:CGSizeMake(310, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
        NSLog(@"high:%f",size.height);
        return size.height + 10;
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary * dict = [mDataArray objectAtIndex:indexPath.row];
    if ([[dict objectForKey:@"is_page"] boolValue]) {
        NSLog(@">>>>>>>>>>");
        return;
    }else{
        NewspaperSingleArticleViewController * newspaperSingleArticleViewController = [[[NewspaperSingleArticleViewController alloc]initWithNibName:@"NewspaperSingleArticleViewController" bundle:nil]autorelease];
        newspaperSingleArticleViewController.baseInfo = dict;
        newspaperSingleArticleViewController.titleStr = self.titleStr;
        [self.navigationController pushViewController:newspaperSingleArticleViewController animated:YES];
    }
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [titleLabel release];
    [mTableView release];
    [super dealloc];
}
- (void)viewDidUnload {
    [titleLabel release];
    titleLabel = nil;
    [mTableView release];
    mTableView = nil;
    [super viewDidUnload];
}
@end
